package com.zhongen.wflow.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.StrUtil;
import com.zhongen.admin.api.entity.SysDept;
import com.zhongen.wflow.bean.do_.DeptDo;
import com.zhongen.wflow.bean.do_.UserDo;
import com.zhongen.wflow.bean.entity.WflowUserAgents;
import com.zhongen.wflow.bean.vo.DeptVo;
import com.zhongen.wflow.bean.vo.OrgTreeVo;
import com.zhongen.wflow.bean.vo.UserAgentVo;
import com.zhongen.wflow.bean.vo.UserVo;
import com.zhongen.wflow.mapper.WflowUserAgentsMapper;
import com.zhongen.wflow.service.OrgRepositoryService;
import com.zhongen.wflow.service.OrgUserAndDeptService;
import com.zhongen.wflow.utils.R;
import com.zhongen.wflow.utils.UserUtil;
import com.zhongen.wflow.workflow.bean.process.OrgUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.stream.Collectors;

/**
 * @author : willian fu
 * @version : 1.0
 */
@Service
public class OrgUserAndDeptServiceImpl implements OrgUserAndDeptService {

	@Autowired
	private OrgRepositoryService orgRepositoryService;

	@Autowired
	private WflowUserAgentsMapper userAgentsMapper;

	private static final String TIME_FORMAT = "yyyy-MM-dd HH:mm";

	/**
	 * 查询组织架构树
	 * @param deptId 部门id
	 * @param type 类型
	 * @return 组织架构树数据
	 */
	@Override
	public Object getOrgTreeData(String deptId, String type) {
		if ("role".equals(type)) {
			return orgRepositoryService.getSysAllRoles()
				.stream()
				.map(r -> OrgTreeVo.builder().id(r.getRoleId()).type("role").name(r.getRoleName()).build());
		}
		else {
			List<OrgTreeVo> orgs = new LinkedList<>(orgRepositoryService.getSubDeptById(deptId));
			SysDept department = orgRepositoryService.getDeptById(deptId);
			if (null != department && ("user".equals(type) || "org".equals(type))) {
				orgs.addAll(orgRepositoryService.selectUsersByDept(deptId).stream().peek(u -> {
					u.setIsLeader(
							StrUtil.isNotBlank(department.getLeader()) && department.getLeader().equals(u.getId()));
				}).sorted(Comparator.comparing(OrgTreeVo::getIsLeader).reversed()).collect(Collectors.toList()));

				orgs = orgs.stream().filter(org -> org.getId() != null && !org.getId().isEmpty() && !"null".equals(org.getId())).collect(Collectors.toList());
			}
			return R.ok(orgs);
		}
	}

	/**
	 * 模糊搜索用户
	 * @param userName 用户名/拼音/首字母
	 * @return 匹配到的用户
	 */
	@Override
	public Object getOrgTreeUser(String userName) {
		return R.ok(orgRepositoryService.selectUsersByPy(userName));
	}

	@Override
	public List<SysDept> getOrgUserDept(String userId) {
		return orgRepositoryService.getDeptsByUser(userId);
	}

	@Override
	public UserAgentVo getUserAgent(String userId) {
		WflowUserAgents userAgent = userAgentsMapper.selectById(userId);
		if (Objects.nonNull(userAgent)) {
			Date time = GregorianCalendar.getInstance().getTime();
			if (userAgent.getEndTime().after(time)) {
				// 在代理期限内或者
				UserDo user = orgRepositoryService.getUserById(userAgent.getAgentUserId());
				return UserAgentVo.builder()
					.effective(time.after(userAgent.getStartTime()) && time.before(userAgent.getEndTime()))
					.user(OrgUser.builder()
						.id(String.valueOf(user.getUserId()))
						.name(user.getUsername())
						.type("user")
						.avatar(user.getAvatar())
						.build())
					.timeRange(CollectionUtil.newArrayList(DateUtil.format(userAgent.getStartTime(), TIME_FORMAT),
							DateUtil.format(userAgent.getEndTime(), TIME_FORMAT)))
					.build();
			}
		}
		return null;
	}

	@Override
	public void setUserAgent(UserAgentVo agent) {
		String userId = UserUtil.getLoginUserId();
		WflowUserAgents userAgent = userAgentsMapper.selectById(userId);
		WflowUserAgents userAgents = WflowUserAgents.builder()
			.userId(userId)
			.agentUserId(agent.getUser().getId())
			.startTime(DateUtil.parse(agent.getTimeRange().get(0), TIME_FORMAT))
			.endTime(DateUtil.parse(agent.getTimeRange().get(1), TIME_FORMAT))
			.createTime(GregorianCalendar.getInstance().getTime())
			.build();
		if (Objects.nonNull(userAgent)) {
			userAgentsMapper.updateById(userAgents);
		}
		else {
			userAgentsMapper.insert(userAgents);
		}
	}

	@Override
	public void cleanUserAgent() {
		userAgentsMapper.deleteById(UserUtil.getLoginUserId());
	}

	@Override
	public String getUserSign() {
		return orgRepositoryService.getUserSign(UserUtil.getLoginUserId());
	}

	@Override
	public UserVo getUserDetail(String userId) {
		UserVo userVo = orgRepositoryService.getUserDetail(userId);
		userVo.setUserAgent(getUserAgent(userId));
		return userVo;
	}

}
